package com.sanchunhui.psc.widgets

import android.content.Context
import android.util.AttributeSet
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import com.ashokvarma.bottomnavigation.ShapeBadgeItem
import com.sanchunhui.psc.R
import com.sanchunhui.psc.common.BaseApplication

/*
    底部导航
 */
class BottomNavBar @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BottomNavigationBar(context, attrs, defStyleAttr) {

    //消息Tab 标签
    private val mMsgBadge:ShapeBadgeItem

    init {
        //消息
        val msgItem = BottomNavigationItem(R.drawable.btn_nav_msg_press,resources.getString(R.string.nav_bar_msg))
                .setInactiveIconResource(R.drawable.btn_nav_msg_normal)
                .setActiveColorResource(R.color.common_blue_deep)
                .setInActiveColorResource(R.color.text_normal)

        mMsgBadge = ShapeBadgeItem()
        mMsgBadge.setShape(ShapeBadgeItem.SHAPE_OVAL)
        msgItem.setBadgeItem(mMsgBadge)

        //组织
        val orgItem = BottomNavigationItem(R.drawable.btn_nav_org_press,resources.getString(R.string.nav_bar_org))
                .setInactiveIconResource(R.drawable.btn_nav_org_normal)
                .setActiveColorResource(R.color.common_blue_deep)
                .setInActiveColorResource(R.color.text_normal)

        //资讯
        val inforItem = BottomNavigationItem(R.drawable.btn_nav_infor_press,resources.getString(R.string.nav_bar_infor))
                .setInactiveIconResource(R.drawable.btn_nav_infor_normal)
                .setActiveColorResource(R.color.common_blue_deep)
                .setInActiveColorResource(R.color.text_normal)

        //我的
        val userItem = BottomNavigationItem(R.drawable.btn_nav_user_press,resources.getString(R.string.nav_bar_user))
                .setInactiveIconResource(R.drawable.btn_nav_user_normal)
                .setActiveColorResource(R.color.common_blue_deep)
                .setInActiveColorResource(R.color.text_normal)

        //设置底部导航模式及样式
        setMode(BottomNavigationBar.MODE_FIXED)
        setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
        setBarBackgroundColor(R.color.common_white)
        val user = BaseApplication.mUser
        if (user.role == 2 || user.role == 1) {
            //添加Tab
            addItem(msgItem)
                .addItem(orgItem)
                .addItem(inforItem)
                .addItem(userItem)
                .setFirstSelectedPosition(0)
                .initialise()
        } else if (user.role == 0) {
            //添加Tab
            addItem(msgItem)
                .addItem(inforItem)
                .addItem(userItem)
                .setFirstSelectedPosition(0)
                .initialise()
        }

    }

    /*
        检查消息Tab是否显示标签
     */
    fun checkMsgBadge(isVisiable:Boolean){
        if (isVisiable){
            mMsgBadge.show()
        }else {
            mMsgBadge.hide()
        }
    }
}
